﻿using Comm;
using Kinlo.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Repo
{
    public class BeforWeightFirstRep
    {
        string strConn;
       // Database DB;
        public BeforWeightFirstRep()
        {
            strConn = System.Configuration.ConfigurationManager.ConnectionStrings["WeightDB"].ConnectionString;
            // DB = (new DbContext("name=WeightDB")).Database;
        }

        public int Add2(BeforWeightFirst w)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {
                    var cmd = @"INSERT dbo.BeforWeight_First(Code ,BeforWeight_Index ,BeforWeight_First ,ProductDate,Weight_OK,IsValidate,[Index],PoleEarOK)
                        VALUES(@Code, @BeforWeight_Index, @BeforWeight_First, @ProductDate,@Weight_OK,@IsValidate,@Index,@PoleEarOK)";

                    var sqlCmd = new SqlCommand(cmd, conn);
                    sqlCmd.Parameters.Add(new SqlParameter("@Code", w.Code == null ? DBNull.Value : (object)w.Code));
                    sqlCmd.Parameters.Add(new SqlParameter("@IsValidate", w.IsValidate == null ? DBNull.Value : (object)w.IsValidate));
                    sqlCmd.Parameters.Add(new SqlParameter("@BeforWeight_Index", w.BeforWeight_Index == null ? DBNull.Value : (object)w.BeforWeight_Index));
                    sqlCmd.Parameters.Add(new SqlParameter("@BeforWeight_First", w.BeforWeight_First == null ? DBNull.Value : (object)w.BeforWeight_First));
                    sqlCmd.Parameters.Add(new SqlParameter("@ProductDate", w.ProductDate == null ? DBNull.Value : (object)w.ProductDate));
                    sqlCmd.Parameters.Add(new SqlParameter("@Weight_OK", w.Weight_OK == null ? DBNull.Value : (object)w.Weight_OK));
                    sqlCmd.Parameters.Add(new SqlParameter("@Index", w.Index == null ? DBNull.Value : (object)w.Index));
                    sqlCmd.Parameters.Add(new SqlParameter("@PoleEarOK", w.PoleEarOK == null ? DBNull.Value : (object)w.PoleEarOK));
                    
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "BeforWeightFirstRep", "Add2", ex);
                return 0;
            } 
        }

        //public int Add(BeforWeightFirst w)
        //{
        //    try
        //    {
        //        var cmd = @"INSERT dbo.BeforWeight_First(Code ,BeforWeight_Index ,BeforWeight_First ,ProductDate,Weight_OK,IsValidate)
        //                VALUES(@Code, @BeforWeight_Index, @BeforWeight_First, @ProductDate,@Weight_OK,@IsValidate)";

        //        return DB.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, cmd,
        //                 new SqlParameter("@Code", w.Code == null ? DBNull.Value : (object)w.Code),
        //                 new SqlParameter("@IsValidate", w.IsValidate == null ? DBNull.Value : (object)w.IsValidate),
        //                 new SqlParameter("@BeforWeight_Index", w.BeforWeight_Index == null ? DBNull.Value : (object)w.BeforWeight_Index),
        //                 new SqlParameter("@BeforWeight_First", w.BeforWeight_First == null ? DBNull.Value : (object)w.BeforWeight_First),
        //                 new SqlParameter("@ProductDate", w.ProductDate == null ? DBNull.Value : (object)w.ProductDate),
        //                 new SqlParameter("@Weight_OK", w.Weight_OK == null ? DBNull.Value : (object)w.Weight_OK)
        //             );
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.Log.ErrorFormat("{0} {1} {2}", "BeforWeightFirstRep", "Add", ex);
        //        return 0;
        //    }
        //}

        /// <summary>
        /// 更新状态 是否已失效
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int Update(string code)
        {
            var cmd = "";
            try
            {
                using (var conn = new SqlConnection(strConn))
                {

                    cmd = string.Format(@"UPDATE BeforWeight_First SET IsValidate=0 WHERE Code='{0}'", code);
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2} {3}", "BeforWeightFirstRep", "Update", cmd, ex);
                return 0;
            }
        }

        //public CodeFirst GetBeforWeightFirst(string code)
        //{
        //    try
        //    {
        //        var cmd = "SELECT * FROM dbo.BeforWeight_First WHERE Code=@Code";
        //        return DB.SqlQuery<CodeFirst>(cmd,
        //            new SqlParameter("@Code", code)).FirstOrDefault();
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.Log.ErrorFormat("{0} {1} {2}", "BeforWeightFirstRep", "GetBeforWeightFirst", ex);
        //        return null;
        //    }
        //}

        //public List<BeforWeightFirst> GetBeforWeight()
        //{
        //    try
        //    {
        //        var cmd = @"SELECT * FROM dbo.BeforWeight_First  WHERE IsValidate=1 ORDER BY ProductDate DESC";
        //        return DB.SqlQuery<BeforWeightFirst>(cmd).ToList();
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.Log.ErrorFormat("{0} {1} {2}", "BeforWeightFirstRep", "GetBeforWeight", ex);
        //        return null;
        //    }
        //}


        public IList<BeforWeightFirst> GetBeforWeight1()
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                { 
                    var cmd = @"SELECT TOP 1000 * FROM dbo.BeforWeight_First WHERE IsValidate=1 AND	ProductDate>DATEADD(DAY,-3,GETDATE()) ORDER BY ProductDate DESC";
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    var BeforWeightFirsts = sqlCmd.ExecuteReader().ReaderToList<BeforWeightFirst>();// 
                    if (BeforWeightFirsts.Count > 0)
                        return BeforWeightFirsts;
                }
                return null;

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "BeforWeightFirstRep", "GetBeforWeight1", ex);
                return null;
            }
        }

        public int DeleteBeforWeight(BeforWeightFirst w)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {

                    var cmd = string.Format(@"DELETE dbo.BeforWeight_First WHERE Code='{0}'", w.Code);
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "BeforWeightFirstRep", "DeleteBeforWeight", ex);
                return 0;
            }
        }
    }

}
